package com.zwk.edu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zwk.base.entity.CommonResult;
import com.zwk.edu.entity.Course;
import com.zwk.edu.entity.Teacher;
import com.zwk.edu.service.CourseService;
import com.zwk.edu.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zwk
 * @create 2022/8/27 18:04
 */
@RestController
@RequestMapping("/edu/front")
@CrossOrigin
public class IndexFrontController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private TeacherService teacherService;

    // 查询前8条热门课程数据, 前4条名师数据
    @GetMapping("/index")
    @Cacheable(key = "'index'", value = "data")
    public CommonResult index(){

        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.orderByDesc("id");
        courseQueryWrapper.last("limit 8");
        List<Course> courses = courseService.list(courseQueryWrapper);

        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.orderByDesc("id");
        teacherQueryWrapper.last("limit 4");
        List<Teacher> teachers = teacherService.list(teacherQueryWrapper);

        return CommonResult.ok().data("eduList", courses).data("teacherList", teachers);
    }

}
